package com.ma.controller;

import com.alibaba.fastjson.JSON;
import com.ma.pojo.Goods;
import com.ma.service.goods.GoodsService;
import com.ma.service.goods.GoodsServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.rmi.MarshalledObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
*
*
@Author: xiaoma
@Date: 09:57 2020/8/26
@Description: 货物有关的CRUD
*/

@Controller
@CrossOrigin
@RequestMapping("/goods")
public class GoodsController {
    private GoodsService goodsService;

    public GoodsController(GoodsService goodsService) {
        this.goodsService = goodsService;
    }
    @RequestMapping("/Count")
    @CrossOrigin
    @ResponseBody
    public Object goodsCount(){
        //用于main页面的商品种类以及商品种类紧缺的个数
        Integer goodsCount = goodsService.queryGoodsCount();
        Integer goodsTight = goodsService.queryGoodsTight();
        System.out.println(goodsCount);
        HashMap<String,Object> map = new HashMap<>();
        map.put("goodsCount",goodsCount);
        map.put("goodsTight",goodsTight);
        JSON json = (JSON) JSON.toJSON(map);
        return json;
    }
    @RequestMapping("/getGoods")
    public String getGoods(Model model){
        //获取商品信息
        List<Goods> list = goodsService.queryAllGoods();
        System.out.println(list);
        model.addAttribute("list",list);
        return "goods";
    }
    @RequestMapping("/del/{goodsId}")
    public String deleteGoods(@PathVariable("goodsId") String goodsId){
        //删除商品信息
        goodsService.deleteGoodById(goodsId);
        return "redirect:/goods/getGoods";
    }
    @RequestMapping("/queryGoods")
    public String queryGoods(String goodsId,Model model){
        System.out.println("=======queryGoods========");
        //多条件查询
        Goods good = goodsService.queryByGoodsId(goodsId);
        List<Goods> list = new ArrayList<>();
        list.add(good);
        System.out.println(good);
        if (good == null){
            list = goodsService.queryAllGoods();
            String msg = new String("无查询结果");
            Model model1 = model.addAttribute("msg", msg);
            System.out.println(model);
        }

        model.addAttribute("list",list);
        return "goods";
    }
    @RequestMapping("/toAddGoods")
    public String toAddGoods(){
        return "addGoods";
    }
    @RequestMapping("/addGoods")
    public String addGoods(Goods goods,Model model) {
        //增加商品
        System.out.println(goods);
        int goods1 = goodsService.addGoods(goods);
        System.out.println(goods1);
        return "redirect:/goods/getGoods";
    }
    @RequestMapping("/toUpdateGoods")
    public String toUpdateGoods(String goodsId, Model model){
        System.out.println("======toUpdateGoods=====");
        System.out.println(goodsId);
        Goods goods = goodsService.queryByGoodsId(goodsId);
        System.out.println(goods);
        model.addAttribute("good",goods);
        return "updateGoods";
    }
    @RequestMapping("/updateGoods")
    public String updateGoods(Goods goods,Model model){
        //修改商品信息
        System.out.println("======updateGoods======");
        System.out.println(goods);
        System.out.println(goods.getGoodsId());
        int updateGoods = goodsService.updateGoods(goods);
        System.out.println(updateGoods);
        Goods good = goodsService.queryByGoodsId(goods.getGoodsId());
        model.addAttribute("goods",good);
        return "redirect:/goods/getGoods";
    }
}
